/**
 * @Project RSLsXWG
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate May 2, 2011
 * @Createtime 11:43:21 PM
 */

package com.xwg.cards
{
	import com.xwg.cards.events.CardEvent;
	import com.xwg.cards.events.CardRenderEvent;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class CardRenderer extends Sprite
	{
		private var _cardBack:CardBackAAA;

		private var _cardFace:CardFaceAAA;

		private var _card:Card;

		public var cardVSO:CardRendererVDO;

		public function CardRenderer(card:Card = null)
		{
			super();

			cardVSO = new CardRendererVDO();

			visible = false; // only visible when on hand

			_card = card;
			_card.addEventListener(CardEvent.CARD_VALUE_CHANGE, cardValueChangeHandler);
			_card.addEventListener(CardEvent.CARD_INTERACTABLE_CHANGE, cardInteractableChangeHandler);
			_card.addEventListener(CardEvent.CARD_VISIBLE_CHANGE, cardVisibleChangeHandler);
			_card.addEventListener(CardEvent.CARD_REVEAL_CARD_CHANGE, cardRevealCardChangeHandler);

			_cardFace = new CardFaceAAA();

			//*
			_cardFace.x = -60;
			_cardFace.y = -80;
			//*/

			/*
			_cardFace.x = -54;
			_cardFace.y = -75;
			//*/

			addChild(_cardFace);

			_cardBack = new CardBackAAA();

			//*
			_cardBack.x = -60;
			_cardBack.y = -80;
			//*/

			/*
			_cardBack.x = -54;
			_cardBack.y = -75;
			//*/

			addChild(_cardBack);
		}

		public function resetData():void
		{
			_card.resetData();

			visible = false;
			alpha = 1;
			x = 0;
			y = 0;
			rotationZ = 0;
		}

		/**
		 * Anti alias
		 */
		override public function set rotationZ(value:Number):void
		{
			rotation = value;
		}

		public function get card():Card
		{
			return _card;
		}

		public function set card(value:Card):void
		{
			_card = value;
		}

		protected function cardVisibleChangeHandler(event:Event):void
		{
			visible = _card.visible;
		}

		protected function cardInteractableChangeHandler(event:Event):void
		{
			if (_card.interactable)
			{
				useHandCursor = true;
				buttonMode = true;

				if (!hasEventListener(MouseEvent.CLICK))
				{
					addEventListener(MouseEvent.CLICK, toggleCardSelection);
				}
			}
			else
			{
				useHandCursor = false;
				buttonMode = false;

				if (hasEventListener(MouseEvent.CLICK))
				{
					removeEventListener(MouseEvent.CLICK, toggleCardSelection);
				}
			}
		}

		protected function cardRevealCardChangeHandler(event:CardEvent):void
		{
			if (_card.revealed)
			{
				_cardBack.alpha = 0;
			}
			else
			{
				_cardBack.alpha = 1;
			}
		}

		public function cardValueChangeHandler(event:CardEvent):void
		{
			var value:int;
			var suit:int;

			switch (_card.value)
			{
				case Card.ACE:
					value = 1;
					break;
				case "2":
				case "3":
				case "4":
				case "5":
				case "6":
				case "7":
				case "8":
				case "9":
				case "10":
					value = int(_card.value);
					break;
				case Card.JACK:
					value = 11;
					break;
				case Card.QUEEN:
					value = 12;
					break;
				case Card.KING:
					value = 13;
					break;
				default:
					value = 0;
					break;
			}

			switch (_card.suit)
			{
				case Card.CLUBS:
					suit = 1;
					break;
				case Card.DIAMONDS:
					suit = 2;
					break;
				case Card.HEARTS:
					suit = 3;
					break;
				case Card.SPADES:
					suit = 4;
					break;
				default:
					suit = 0;
					break;
			}

			var des:int = value + (suit - 1) * 13;
			_cardFace.gotoAndStop(des);
		}

		public function toggleCardSelection(event:MouseEvent):void
		{
			_card.toggleSelection();

			var e:CardRenderEvent = new CardRenderEvent(CardRenderEvent.CARD_CLICK, null, true, true);
			dispatchEvent(e);
		}
	}
}
